package com.xjrsoft.module.customerTwo.AppManage.sales_order.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaMode;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xjrsoft.common.page.PageOutput;
import com.xjrsoft.common.page.ConventPage;

import java.util.ArrayList;
import java.util.List;

import com.xjrsoft.common.utils.DataTransUtil;
import com.xjrsoft.common.result.Response;
import com.xjrsoft.core.tool.utils.BeanUtil;
import com.xjrsoft.core.tool.utils.Func;
import com.xjrsoft.module.base.service.IXjrBaseCodeRuleService;
import com.xjrsoft.module.customerTwo.AppManage.business_opportunity.entity.BusinessOpportunityLog;
import com.xjrsoft.module.customerTwo.AppManage.business_opportunity.service.IBusinessOpportunityLogService;
import com.xjrsoft.module.customerTwo.AppManage.business_opportunity.vo.BusinessOpportunityLogVo;
import com.xjrsoft.module.customerTwo.AppManage.sales_order.dto.SalesOrderDto;
import com.xjrsoft.module.customerTwo.AppManage.sales_order.dto.SaveSalesOrderFormDataDto;
import com.xjrsoft.module.customerTwo.AppManage.sales_order.vo.SalesOrderVo;
import com.xjrsoft.module.customerTwo.AppManage.sales_order.vo.SalesOrderListVo;
import com.xjrsoft.module.customerTwo.AppManage.sales_order.dto.SalesOrderListDto;
import com.xjrsoft.core.secure.utils.SecureUtil;
import com.xjrsoft.core.tool.utils.FlowableUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;

import org.springframework.web.bind.annotation.*;
import com.xjrsoft.module.customerTwo.AppManage.sales_order.entity.SalesOrder;
import com.xjrsoft.module.customerTwo.AppManage.sales_order.service.ISalesOrderService;

/**
 *  控制器
 *
 * @author lisen
 * @since 2024-04-25
 */
@RestController
@AllArgsConstructor
@RequestMapping("/sales_order")
@Api(value = "销售订单", tags = "销售订单接口")
public class SalesOrderController {


	private final ISalesOrderService salesOrderService;

	private final IXjrBaseCodeRuleService codeRuleService;

	private final IBusinessOpportunityLogService businessOpportunityLogService;
	/**
	 * 详情 [{fieldName=sales_department_id, lazyType=department}]
	 */
	@GetMapping("/{id}")
	@ApiOperation(value = "详情", notes = "传入salesOrder")
	//@SaCheckPermission(value = {"SalesContract:view", "SalesContract:modify"}, mode = SaMode.OR)
	@SaCheckLogin
	public Response<SalesOrderVo> getFormData(@PathVariable String id) {
		// 主表数据
		SalesOrder salesOrder = salesOrderService.getById(id);
		SalesOrderVo salesOrderVo = BeanUtil.copy(salesOrder, SalesOrderVo.class);
		salesOrderVo.setSalesDepartmentIdName(DataTransUtil.getDepartmentNameById(salesOrderVo.getSalesDepartmentId()));
		List<String> idList = Func.toStrList(salesOrder.getBusinessOpportunityNumber());
		List<BusinessOpportunityLogVo> businessOpportunityLogVoList = new ArrayList<>();
		idList.forEach(number->{
			BusinessOpportunityLog businessOpportunityLog = businessOpportunityLogService.getByNumber(number);
			BusinessOpportunityLogVo businessOpportunityLogVo = BeanUtil.copy(businessOpportunityLog, BusinessOpportunityLogVo.class);
			businessOpportunityLogVoList.add(businessOpportunityLogVo);
		});
		salesOrderVo.setBusinessOpportunityLogVos(businessOpportunityLogVoList);
		return Response.ok(salesOrderVo);
	}

	/**
	 * 自定义分页 
	 */
	@GetMapping
	@ApiOperation(value = "分页", notes = "传入salesOrder")
		public Response<PageOutput<SalesOrderListVo>> getPageList(SalesOrderListDto listDto) {
		IPage<SalesOrder> page = salesOrderService.getPageList(listDto);
		List<SalesOrderListVo> records = BeanUtil.copyList(page.getRecords(), SalesOrderListVo.class);
			// 转换列表数据
		DataTransUtil.transListShowData(records);
			// 工作流数据处理
		FlowableUtil.systemFormList(records, SecureUtil.getModuleId());
		return Response.ok(ConventPage.getPageOutput(page.getTotal(), records));
	}

	/**
	 * 新增 
	 */
	@PostMapping
	@ApiOperation(value = "新增", notes = "传入salesOrder")
	//@SaCheckPermission("SalesContract:add")
	@SaCheckLogin
	public Response save(@RequestBody SaveSalesOrderFormDataDto formDto) {
		SalesOrderDto salesOrderDto = formDto.getSalesOrderDto();
		SalesOrder salesOrder = BeanUtil.copy(salesOrderDto, SalesOrder.class);
		Integer dataId =  salesOrderService.addSalesOrder(salesOrder);
		if (dataId != null) {
			codeRuleService.useEncode("SalesOrderCode");
		}
		return dataId == null ? Response.notOk() : Response.ok(dataId);
	}

	/**
	 * 修改 
	 */
	@PutMapping("/{id}")
	@ApiOperation(value = "修改", notes = "传入salesOrder")
	//@SaCheckPermission("SalesContract:modify")
	@SaCheckLogin
	public Response update(@PathVariable String id, @RequestBody SaveSalesOrderFormDataDto formDto) {
		SalesOrderDto salesOrderDto = formDto.getSalesOrderDto();
		SalesOrder salesOrder = BeanUtil.copy(salesOrderDto, SalesOrder.class);
		return Response.status(salesOrderService.updateSalesOrder(id, salesOrder));
	}



	/**
	 * 删除 
	 */
	@DeleteMapping("/{ids}")
	@ApiOperation(value = "删除", notes = "传入ids")
	//@SaCheckPermission(value = {"SalesContract:batchDelete", "SalesContract:delete"}, mode = SaMode.OR)
	@SaCheckLogin
	public Response remove(@ApiParam(value = "主键集合", required = true) @PathVariable String ids) {
		return Response.status(salesOrderService.delete(ids));
	}


}
